package cn.e4j.modular.system.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.TreeUtil;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysOrg;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import cn.e4j.modular.system.service.SysOrgService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 组织管理
 *
 * @author yuan_rz
 * @Date 2019/01/02 10:22
 */
@Controller
@RequestMapping("/sys/org")
public class SysOrgController extends BaseController {

    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:org:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/org.html";
    }

    /**
     * 查询部门列表--树
     */
    @RequestMapping("/getOrgsTree")
    @RequiresPermissions("user")
    //@Cacheable(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_ORG_GETORGSTREE + "'" )
    @ResponseBody
    public ResponseData getOrgsTree() {
        List<SysOrg> sysOrgs = this.sysOrgService.getOrgsTree();
        return ResponseData.success(TreeUtil.orgsTree(sysOrgs));
    }

    /**
     * 查询组织列表
     */
    @RequestMapping("/getOrgsList")
    @RequiresPermissions("sys:org:init")
    @ResponseBody
    public ResponseData getOrgsList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        inDto.put("is_del", "0");
        List<Map<String, Object>> result = sysOrgService.getOrgsList(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping(value="/exportOrgs",method=RequestMethod.GET)
    @RequiresPermissions("sys:org:init")
    public void exportOrgs(ModelMap map, HttpServletRequest request, HttpServletResponse response){
        List<SysOrg> list = sysOrgMapper.getOrgs(null);
        ExportParams params = new ExportParams(null, "组织数据", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "组织数据");
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, SysOrg.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:org:init")
    @ResponseBody
    public ResponseData add(SysOrg sysOrg, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Dto node = sysOrgService.add(sysOrg);

        //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_ORG_GETORGSTREE);

        //返回
        return new SuccessResponseData(node);
    }

    /**
     * 获取同级组织下的最大排序号
     */
    @RequestMapping("/getSortNo")
    @RequiresPermissions("sys:org:init")
    @ResponseBody
    public ResponseData getSortNo(String ParentId) {
        Long sortNo = sysOrgMapper.getSortNo(ParentId);
        if(sortNo == null) {
            sortNo = 0L;
        }
        return new SuccessResponseData(sortNo+1);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:org:init")
    @ResponseBody
    public ResponseData del(SysOrg sysOrg, BindingResult result) {
        List childList = sysOrgService.getChildList(sysOrg);
        if(childList.size() > 0){
            return new ErrorResponseData(BizExceptionEnum.ORG_DELETE_ERROR.getCode(),
                    BizExceptionEnum.ORG_DELETE_ERROR.getMessage());
        }else {
            sysOrgService.delete(sysOrg);
            //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_ORG_GETORGSTREE);
            return SUCCESS_TIP;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:org:init")
    @ResponseBody
    public ResponseData edit(SysOrg sysOrg, BindingResult result) {
        sysOrgService.edit(sysOrg);
        //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_ORG_GETORGSTREE);
        return SUCCESS_TIP;
    }


}
